<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请人" prop="applicant">
        <el-input
          v-model="queryParams.applicant"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提现方式" prop="withdrawalMethod">
        <el-select v-model="queryParams.withdrawalMethod" placeholder="请选择提现方式" clearable @change="handleQuery">
          <el-option label="微信" value="1"></el-option>
          <el-option label="支付宝" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable @change="handleQuery">
          <el-option label="待审核" value="1"></el-option>
          <el-option label="待打款" value="2"></el-option>
          <el-option label="已打款" value="3"></el-option>
          <el-option label="拒绝" value="4"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table border stripev-loading="loading" :data="withdrawalList" @selection-change="handleSelectionChange">
      <el-table-column label="申请编号" align="center" prop="id"/>
      <el-table-column label="用户昵称" align="center" prop="nickName"/>
      <el-table-column label="收款账号名称" align="center" prop="accountNumber"/>
      <el-table-column label="申请时间" align="center" prop="createTime"/>
      <el-table-column label="申请金额" align="center" prop="actualPrice"/>
      <el-table-column label="提现方式" align="center" prop="methods">
        <template slot-scope="scope">
          <span v-if="scope.row.method == 1">支付宝</span>
          <span v-if="scope.row.method == 2">微信</span>
        </template>
      </el-table-column>
      <el-table-column label="收款账号" align="center" prop="accountNumber"/>
      <el-table-column label="状态" align="center" prop="process">
        <template slot-scope="scope">
          <span v-if="scope.row.process == 0">待审核</span>
          <span v-if="scope.row.process == 1">待打款</span>
          <span v-if="scope.row.process == 2">已打款</span>
          <span v-if="scope.row.process == 3">已拒绝</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:withdrawal:edit']">修改</el-button>-->
          <!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:withdrawal:remove']">删除</el-button>-->
          <el-button size="mini" type="text" @click="agree(scope.row,1)" v-show="scope.row.process == 0">同意</el-button>
          <el-button size="mini" type="text" @click="refuse(scope.row)" v-show="scope.row.process == 0">拒绝</el-button>
          <el-button size="mini" type="text" @click="agree(scope.row,2)" v-show="scope.row.process == 1">确认打款</el-button>
          <span v-show="scope.row.process == 2">已打款</span>
          <span v-show="scope.row.process == 3">已拒绝</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改提现对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号"/>
        </el-form-item>
        <el-form-item label="提现金额" prop="actualPrice">
          <el-input v-model="form.actualPrice" placeholder="请输入提现金额"/>
        </el-form-item>
        <el-form-item label="支付金额" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入支付金额"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addWithdrawal,
  delWithdrawal,
  getWithdrawal,
  listWithdrawal,
  updateWithdrawal,
  updateWithdrawalState
} from "@/api/withdrawal";

export default {
  name: "Withdrawal",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 提现表格数据
      withdrawalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        withdrawalMethod: null,
        applicant: null,
        orderNo: null,
        userId: null,
        actualPrice: null,
        status: null,
        totalPrice: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    agree(row,process) {
      this.$confirm('确认同意该提现申请吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'success'
      }).then(() => {
        updateWithdrawalState({
          id: row.id, process
        }).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.getList();
        })
      });
    },
    refuse(row) {
      this.$prompt('请输入拒绝原因', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputPattern: /^[a-zA-Z0-9]+$/,
        inputErrorMessage: "拒绝原因不能为空",
      }).then(res=>{
        updateWithdrawalState({
          id: row.id, process: 3,reason:res.value
        }).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.getList();
        })
      })
    },
    /** 查询提现列表 */
    getList() {
      this.loading = true;
      listWithdrawal(this.queryParams).then(response => {
        this.withdrawalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        userId: null,
        actualPrice: null,
        status: null,
        totalPrice: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加提现";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWithdrawal(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改提现";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWithdrawal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWithdrawal(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除提现编号为"' + ids + '"的数据项？').then(function () {
        return delWithdrawal(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/withdrawal/export', {
        ...this.queryParams
      }, `withdrawal_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
